package com.xielaoban.cqueshop.Admin.Controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xielaoban.cqueshop.Common.Result;
import com.xielaoban.cqueshop.Controller.NewsController;
import com.xielaoban.cqueshop.Entity.News;
import com.xielaoban.cqueshop.Service.NewsService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 蟹老板
 * @Date 2021-4-29 13:13
 * @Version 1.0
 * @Package com.xielaoban.cqueshop.Admin.Controller
 * @Description
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/news")
public class AdminNewsController {
    private static final Log log = LogFactory.getLog(NewsController.class);
    @Autowired
    private NewsService newsService;

    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        try {
            log.info("获取公告信息！" + id);
            News news = newsService.get(id);
            return Result.Success(news);
        } catch (Exception e) {
            log.info("获取公告信息出错了！", e);
            return Result.Error();
        }
    }

    @PostMapping("/getAll")
    public Result getAll(@RequestBody JSONObject queryInfo) {
        try {
            log.info("获取所有公告信息！");
            int pageSize = queryInfo.getInteger("pageSize");
            int currentPage = queryInfo.getInteger("currentPage");
            PageInfo<News> newsPageInfo = newsService.getAll(pageSize, currentPage);
            return Result.Success(newsPageInfo);
        } catch (Exception e) {
            log.info("获取所有公告信息出错了！", e);
            return Result.Error();
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody News news) {
        try {
            log.info("添加公告信息！");
            int result = newsService.add(news);
            if (result > 0) {
                return Result.Success();
            } else {
                return Result.Error();
            }
        } catch (Exception e) {
            log.info("添加公告信息出错了！", e);
            return Result.Error();
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody News news) {
        try {
            log.info("更新公告信息！");
            int result = newsService.update(news);
            if (result > 0) {
                return Result.Success(result);
            } else {
                return Result.Error();
            }
        } catch (Exception e) {
            log.info("更新公告信息出错了！", e);
            return Result.Error();
        }
    }

    @GetMapping("/updateStatus")
    public Result updateStatus(String id, Integer status) {
        try {
            log.info("更新公告状态信息！");
            int result = newsService.updateStatus(id, status);
            if (result > 0) {
                return Result.Success(result);
            } else {
                return Result.Error();
            }
        } catch (Exception e) {
            log.info("更新公告状态信息出错了！", e);
            return Result.Error();
        }
    }


    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable String id) {
        try {
            log.info("删除公告信息！");
            int result = newsService.del(id);
            if (result > 0) {
                return Result.Success(result);
            } else {
                return Result.Error();
            }
        } catch (Exception e) {
            log.info("删除公告信息出错了！", e);
            return Result.Error();
        }
    }

}
